package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RankClassAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.db.SongListEntity;
import com.lanou.sfu.songforu.entity.SongMenuDetailEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.tools.ListViewForScrollView;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.ArrayList;
import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/31.
 */
public class SongMenuDetailFragment extends BaseFragment implements RankClassAdapter.OnClickToShareLinstener{
    private View v;
    private ListViewForScrollView listView;
    private VolleySingleton requestQueue;
    private StringRequest request;
    private Context context;
    private List<SongMenuDetailEntity.ContentEntity> entityList;
    private List<SongListEntity> changeList;
    private TextView style,title,listenerNum,collectNum,songMenuTotal;
    private ImageView imageView;
    private UMSocialService mController;
    private ImageButton share,download, playAll;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_songmenu_detail, null);
        init();

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Qzone分享
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(), getString(R.string.qQ_appID), getString(R.string.qQ_appSecret));
        qZoneSsoHandler.addToSocialSDK();

        // qq分享
        UMQQSsoHandler qqssoH = new UMQQSsoHandler(getActivity(), getString(R.string.qQ_appID), getString(R.string.qQ_appSecret));
        qqssoH.addToSocialSDK();

        // 设置分享内容
        QQShareContent qqcontent = new QQShareContent();
        qqcontent.setShareContent(getString(R.string.RANK_CLASS_SHARE_CONTENT));
        mController.setShareMedia(qqcontent);

        // 给页面设置工具栏
        final Toolbar toolbar = (Toolbar) v.findViewById(R.id.songmenu_detail_toolbar);
        toolbar.setNavigationIcon(R.mipmap.bt_return_nor);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });

        // 设置工具栏标题
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) v.findViewById(R.id.songmenu_detail_collapsing_toolbar);
        collapsingToolbar.setContentScrimColor(0xf48852);
        Bundle bundle = getArguments();
        String listId = bundle.getString(context.getString(R.string.listId));
        String url = Url.SONG_MENU_LISTID + listId;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongMenuDetailEntity songMenuDetailEntity = gson.fromJson(response, SongMenuDetailEntity.class);
                SongMenuDetailFragment.this.entityList = songMenuDetailEntity.getContent();
                // 遍历加入数据库的实体类集合
                for (SongMenuDetailEntity.ContentEntity entity : entityList){
                    SongListEntity songListEntity = new SongListEntity();
                    songListEntity.setAlbum_id(entity.getAlbum_id());
                    songListEntity.setAll_artist_id(entity.getAll_artist_id());
                    songListEntity.setTitle(entity.getTitle());
                    songListEntity.setTing_uid(entity.getTing_uid());
                    songListEntity.setAuthor(entity.getAuthor());
                    songListEntity.setSong_id(entity.getSong_id());
                    songListEntity.setArtist_name(entity.getAuthor());
                    changeList.add(songListEntity);
                }
                RankClassAdapter adapter = new RankClassAdapter(context,changeList);
                adapter.setOnClickToShareLinstener(SongMenuDetailFragment.this);
                listView.setAdapter(adapter);
                songMenuTotal.setText(entityList.size() + "");
                style.setText(songMenuDetailEntity.getTag());
                title.setText(songMenuDetailEntity.getTitle());
                collectNum.setText(songMenuDetailEntity.getCollectnum());
                listenerNum.setText(songMenuDetailEntity.getListenum());
                Log.i("hha", "" + songMenuDetailEntity.getPic_w700());
                Picasso.with(context).load(songMenuDetailEntity.getPic_w700()).into(imageView);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.addQueue(request);

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mController.openShare(getActivity(), false);
            }
        });

        playAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (SongMenuDetailEntity.ContentEntity entity : entityList) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.play(lists, 0);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (SongMenuDetailEntity.ContentEntity entity : entityList) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.play(lists, position);
            }
        });
    }
    public void init(){
        context = getActivity();
        requestQueue = VolleySingleton.getInstance(context);
        listView = (ListViewForScrollView) v.findViewById(R.id.songmenu_detail_listView);
        style = (TextView) v.findViewById(R.id.songmenu_detail_style);
        title = (TextView) v.findViewById(R.id.songmenu_detail_info);
        collectNum = (TextView) v.findViewById(R.id.songmenu_detail_collectNum);
        listenerNum = (TextView) v.findViewById(R.id.songmenu_detail_listenerNum);
        imageView = (ImageView) v.findViewById(R.id.songmenu_detail_backdrop);
        share = (ImageButton) v.findViewById(R.id.songmenu_detail_share);
        changeList = new ArrayList<>();
        mController = UMServiceFactory.getUMSocialService(getString(R.string.UMSocialService));
        playAll = (ImageButton) v.findViewById(R.id.playAll_imageButton);
        songMenuTotal = (TextView) v.findViewById(R.id.songmenu_total);
    }

    @Override
    public void onClickToShare(SongListEntity songListEntity, int position) {
        mController.openShare(getActivity(), false);
    }
}
